package com.atguigu.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.OrderSettingDao;
import com.atguigu.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl  implements OrderSettingService{
    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public void importData(List<OrderSetting> orderSettingList) {
        for (OrderSetting orderSetting : orderSettingList) {
           Integer count = orderSettingDao.getCountByData(orderSetting.getOrderDate());
            if (count > 0){
                orderSettingDao.edit(orderSetting);
            }else {
                orderSettingDao.add(orderSetting);
            }

        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) { //查询日期格式 yyyy-MM%

        List<Map> mapList = new ArrayList<>();
        List<OrderSetting> orderSettingList =orderSettingDao.getOrderSettingByMonth(date);
        for (OrderSetting orderSetting : orderSettingList) {
            Map map = new HashMap();
            map.put("date",orderSetting.getOrderDate().getDate());
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //查询预约的信息(根据OrderDate)
       OrderSetting orderSettingdb =orderSettingDao.getOrderSetting(orderSetting.getOrderDate());
       if (orderSettingdb == null){
           orderSettingDao.add(orderSetting);
       }else {
           if (orderSetting.getNumber() <= orderSetting.getReservations()){
               new  RuntimeException("数据异常，禁止修改");
           }else {
               orderSettingDao.edit(orderSetting);
           }
       }
    }
}
